﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIS.UI;
using SIS.ENT;
using SIS.BLL;


public partial class Employee_EmployeeTimeSheetEntry : System.Web.UI.Page
{
    public Common _common = new Common();

    public Int32 _SchoolKey = -1;

    protected void Page_Load(object sender, EventArgs e)
    {

        lblTopMessage.Text = DateTime.Now.ToString();
        if (!Page.IsPostBack)
        {
            if (Request.Params["SchoolKey"] != null)
            {
                _SchoolKey = Convert.ToInt32(Request.Params["SchoolKey"].ToString());
                _common.LoadSchoolDDL(ddlSchoolList, Convert.ToString(_SchoolKey));
              
            }
            else
            {
                _common.LoadSchoolDDL(ddlSchoolList, string.Empty);
                _SchoolKey = Convert.ToInt32(ddlSchoolList.SelectedValue);
            }
            _common.LoadDayCodeDDL(ddlDayCode, string.Empty);
            _common.LoadEmployeeDDL(ddlEmployee, _SchoolKey, string.Empty);

            if (Request.Params["id"] == null)
            {
                objStart.Text = DateTime.Today;
                objEnd.Text = DateTime.Today;
                objStart.Hour = DateTime.Now.Hour.ToString();
                objStart.Minute = DateTime.Now.Minute.ToString();
                objEnd.Hour = DateTime.Now.Hour.ToString();
                objEnd.Minute = DateTime.Now.Minute.ToString();
                btnSingOut.Enabled = false;
                ddlDayCode.SelectedValue = GetTodayDate();
            }
            else
            {
                lblEmployeeTimeSheetKey.Text = Request.Params["id"];
                if (Request.Params["AttandanceStatus"] != null)
                {
                    bool AttandanceStatus = Convert.ToBoolean(Request.Params["AttandanceStatus"]);
                    if (AttandanceStatus == true)
                    {
                        btnSingIn.Enabled = false;
                        btnSingOut.Enabled = false;
                    }
                    else
                    {
                        btnSingIn.Enabled = false;
                    }

                }

                FillValue(Convert.ToInt64(lblEmployeeTimeSheetKey.Text));
            }

        }
    }

    private string GetTodayDate()
    {
        string TodayDay = "";
        switch (DateTime.Today.DayOfWeek.ToString())
        {
            case   "Monday" :
                TodayDay = "1" ;
                break ;
            case "Tuesday":
                TodayDay = "2";
                break;
            case "Wednesday":
                TodayDay = "3";
                break;
            case "Thursday":
                TodayDay = "4";
                break;
            case "Friday":
                TodayDay = "5";
                break;
            case "Saturday":
                TodayDay = "6";
                break;
            default:
                break;
        }
        return TodayDay;
    }

    private void FillValue(long EmployeeTimeSheetKey)
    {
        EmployeeTimeSheetTable objEmployeeTimeSheet = new  EmployeeTimeSheetTable();
        EmployeeProfileTable objEmployeeProfileKey = new EmployeeProfileTable();

        objEmployeeTimeSheet = EmployeeTimeSheetTableManager.GetItem(EmployeeTimeSheetKey);
        objEmployeeProfileKey = EmployeeProfileTableManager.GetItem(objEmployeeTimeSheet.EmployeeProfileKey, Convert.ToInt32(ddlSchoolList.SelectedValue));
        if (objEmployeeTimeSheet != null)
        {
            ddlSchoolList.SelectedValue = Convert.ToString(objEmployeeTimeSheet.SchoolKey);
            ddlEmployee.SelectedValue = Convert.ToString(objEmployeeProfileKey.UserAccountKey);
            ddlDayCode.SelectedValue = Convert.ToString(objEmployeeTimeSheet.DayCode);
            objStart.Text = objEmployeeTimeSheet.TimeIn;
            objEnd.Text = objEmployeeTimeSheet.TimeOut;
            txtRemark.Text = objEmployeeTimeSheet.Remarks;
            lblEmployeeTimeSheetKey.Text = Convert.ToString(objEmployeeTimeSheet.EmployeeTimeSheetKey);
            lblUserAccountKey.Text = Convert.ToString(objEmployeeTimeSheet.UserAccountKey);
            lblEmployeeProfileKey.Text = Convert.ToString(objEmployeeTimeSheet.EmployeeProfileKey);
            bool TimeSheetStatus = Convert.ToBoolean(objEmployeeTimeSheet.TimeSheetStatus);
            if (TimeSheetStatus)
            {
                btnSingIn.Enabled = false;
                btnSingOut.Enabled = false;
            }
            else {
                btnSingIn.Enabled = false;
                
            }
        }
    }

    protected void btnSingIn_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            long EmployeeTimeSheetKey = Convert.ToInt64(lblEmployeeTimeSheetKey.Text);
            string Remarks = txtRemark.Text;
            EmployeeTimeSheetTable objEmployeeTimeSheet = new EmployeeTimeSheetTable();
            EmployeeProfileTable objEmplyee = new EmployeeProfileTable();
            objEmplyee = EmployeeProfileTableDB.GetItemByUserAccountKey(Convert.ToInt32(ddlEmployee.SelectedValue), Convert.ToInt32(ddlSchoolList.SelectedValue));
            objEmployeeTimeSheet.Employee = ddlEmployee.SelectedValue;
            objEmployeeTimeSheet.EmployeeProfileKey = objEmplyee.EmployeeProfileKey;
            objEmployeeTimeSheet.UserAccountKey = objEmplyee.UserAccountKey;
            objEmployeeTimeSheet.DayCode = Convert.ToInt16(ddlDayCode.SelectedValue);
            objEmployeeTimeSheet.TimeIn = DateTime.Now;
            objEmployeeTimeSheet.TimeOut = DateTime.Now;
            objEmployeeTimeSheet.EmployeeTimeSheetKey = EmployeeTimeSheetKey;
            objEmployeeTimeSheet.TimeSheetStatus = false;
            objEmployeeTimeSheet.Status = 1;
            objEmployeeTimeSheet.Remarks = txtRemark.Text;
            objEmployeeTimeSheet.CreatedDateTime = DateTime.Now;
            objEmployeeTimeSheet.ModifiedDateTime = DateTime.Now;

            EmployeeTimeSheetKey = EmployeeTimeSheetTableManager.Save(objEmployeeTimeSheet);
            if (EmployeeTimeSheetKey > 0)
            {
                lblMessage.Text = "TimeSheet Record Saved";
            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("EmployeeTimeSheet-List.aspx");
    }

    protected void btnViewEmployeeList_Click(object sender, EventArgs e)
    {
        Response.Redirect("Employees-List.aspx");
    }

    protected void btnAddNewEmployee_Click(object sender, EventArgs e)
    {
        Response.Redirect("EmployeeEntry.aspx");
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnSingOut_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            long lReturn = 0;
            long EmployeeTimeSheetKey = Convert.ToInt64(lblEmployeeTimeSheetKey.Text);
            string Remarks = txtRemark.Text;
            EmployeeTimeSheetTable objEmployeeTimeSheet = new EmployeeTimeSheetTable();
            EmployeeProfileTable objEmplyee = new EmployeeProfileTable();
            objEmplyee = EmployeeProfileTableDB.GetItemByUserAccountKey(Convert.ToInt32(ddlEmployee.SelectedValue), Convert.ToInt32(ddlSchoolList.SelectedValue));
            objEmployeeTimeSheet.Employee = ddlEmployee.SelectedValue;
            objEmployeeTimeSheet.EmployeeProfileKey = objEmplyee.EmployeeProfileKey;
            objEmployeeTimeSheet.UserAccountKey = objEmplyee.UserAccountKey;
            objEmployeeTimeSheet.DayCode = Convert.ToInt16(ddlDayCode.SelectedValue);
            objEmployeeTimeSheet.CreatedDateTime = DateTime.Now;
            objEmployeeTimeSheet.ModifiedDateTime = DateTime.Now;
            objEmployeeTimeSheet.Remarks = Remarks;
            objEmployeeTimeSheet.TimeSheetStatus = true;
            objEmployeeTimeSheet.EmployeeTimeSheetKey = EmployeeTimeSheetKey;
            EmployeeTimeSheetTableList _objList = new EmployeeTimeSheetTableList();
            _objList = EmployeeTimeSheetTableManager.GetList(Convert.ToInt32(ddlSchoolList.SelectedValue), Convert.ToString(objEmployeeTimeSheet.EmployeeProfileKey), DateTime.Today, DateTime.Today);
            foreach (EmployeeTimeSheetTable item in _objList)
            {
                objEmployeeTimeSheet.TimeOut = item.TimeOut;
                if (objEmployeeTimeSheet.TimeOut == item.TimeOut)
                {
                    objEmployeeTimeSheet.TimeIn = item.TimeIn;
                    objEmployeeTimeSheet.TimeOut = DateTime.Now;
                    lReturn = EmployeeTimeSheetTableManager.Save(objEmployeeTimeSheet);
                    if (lReturn > 0)
                    {
                        lblMessage.Text = "Attandance Record Updated";
                    }
                    else
                    {
                        lblMessage.Text = "Attandance Record Updated Fail";
                    }
                }
            }

        }
    }
}